www.gusucode.com > 基于粒子滤波的故障检测,使用似然函数作为检测函数 > 基于粒子滤波的故障检测,使用似然函数作为检测函数/code/FDI based on SIR likelihood/ceshi1.m
close all; clear all; n = 1:600;%sample steps stdw = sqrt(10); ngrid = 50; npar = 500;%particle number N = length(n); %N=500 % Generate the state process and observations x0 = 0.1; c0 = 1; b0=25; xpath = zeros(1,N); xmean=0;xvariance=0.1; ymean=0;yvariance=1; xnoise=gauss(xmean,xvariance,N); ynoise=gauss(ymean,yvariance,N); b=b0; xpath(1) = x0/2 + b*x0/(1+x0^2) +8*cos(0) +xnoise(1) ; for i=2:N if i<301 b=b0; else b=b0*5; end xpath(i) = xpath(i-1)/2 + b*xpath(i-1)/(1+xpath(i-1)^2) +8*cos(1.2*(i-1)) + xnoise(i); end zpath = 1/20*(xpath.^2) + ynoise; % Particle filter with resampling w = ones(npar,1)/npar; xprev = randn(npar, 1); SParMat = zeros(npar, N); SXParMat = zeros(npar, N); sxparpath = zeros(1,N); likelihood=zeros(N,1); D=zeros(N,1); for i=1:N xnext = drawpar(xprev, stdw, i); xs = (xnext.^2)/20; w = w.*(1/sqrt(2*pi)*exp(-((zpath(i)-xs).^2)/2)); l=1/sqrt(2*pi)*exp(-((zpath(i)-xs).^2)/2); Li=sum(l)/npar;%i时刻的所有粒子似然函数值均值 likelihood(i)=Li; Di=0; if i<20 for j=1:i Di=Di+(-(log(likelihood(j)))); end else for j=i-20+1:i Di=Di+(-(log(likelihood(j)))); end end D(i)=Di; w = w/sum(w); SParMat(:,i) = w; SXParMat(:,i) = xnext; sxparpath(i) = w'*xnext; [xprev, w] = impResample(xnext, w); end ErrorSIR = sqrt(1/N*(sxparpath-xpath)*(sxparpath-xpath)'); figure; plot(D); %xlabel('time'); %ylabel('x value'); %title('SIR Particle filter'); %legend('Actual', 'Estimate');